<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-serve' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加服务类别</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="serveForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="服务类编号" prop="serveId">
        <el-input v-model="serveForm.serveId" style="width: 400px"></el-input>
      </el-form-item>


      <el-form-item label="服务类名称" prop="serveName">
        <el-input v-model="serveForm.serveName" style="width: 400px"></el-input>
      </el-form-item>

      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="serveForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ccc">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleList: [],
      serveForm: {
        serveId: '',
        serveName: '',
        enable: '',
      },
      rules: {
        serveName: [
          {required: true, message: '请输入服务类名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        serveId: [
          {required: true, message: '请输入服务类ID', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
            ],
        enable: [
          {required: true, message: '', trigger: 'change'}
        ],
      }
    };
  },
  methods: {
    loadserveList() {
      console.log('loadserveList ...');
      let url = "http://localhost:9081/serve";
      console.log(url);
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .get(url).then((respose) => {
        let responseBody = respose.data;
        this.tableData = responseBody.data;
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/serve/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.serveForm);
          console.log('formData：' + formData);
          this.axios
              .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
              .post(url, formData).then((response)=> {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加服务类别成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
            this.$router.push('/sys-serve/temp/serve/list')
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadserveList();
  }
}
</script>

<style scoped>

</style>